package com.test.springboot;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import org.springframework.context.annotation.ComponentScan;

import javax.servlet.ServletException;
import java.io.File;

/**
 * @author yetianhua
 * @description:
 * @create: 2021-07-12 07:30
 */
public class SpringApplication {

    public static void run(Class<?> primarySource, String... args){
        try {
            // 获取需要扫描的包，存储到
            ComponentScan annotation = primarySource.getAnnotation(ComponentScan.class);
            String[] basePackages = annotation.basePackages();
            BasePackage.setBasePackage(basePackages);
            //  1. 构建tomcat服务器，并启动
            Tomcat tomcat = new Tomcat();
            // 创建临时文件
            File file = new File("D:\\temp");
            if(!file.exists()){
                file.mkdirs();
            }
            tomcat.addWebapp("/","D:\\temp");
            tomcat.start();
            tomcat.getServer().await();
        } catch (LifecycleException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }

}
